package org.jeecg.demo.wx.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.demo.wx.dto.QueryIdDto;
import org.jeecg.demo.wx.dto.QueryTaskDto;
import org.jeecg.demo.wx.dto.QueryUserTaskInfoDto;
import org.jeecg.demo.wx.dto.RlSchedulePageDto;
import org.jeecg.demo.wx.entity.RlSchedule;
import org.jeecg.demo.wx.enumeration.StateEnum;
import org.jeecg.demo.wx.service.IRlScheduleService;
import org.jeecg.demo.wx.util.PageData;
import org.jeecg.demo.wx.vo.QueryScheduleVO;
import org.jeecg.demo.wx.vo.WorkHandlingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@Api(tags = "rl_schedule")
@RestController
@RequestMapping("/calendar/rlSchedule")
@Slf4j
public class RlScheduleController extends JeecgController<RlSchedule, IRlScheduleService> {
    @Autowired
    private IRlScheduleService rlScheduleService;

    /**
     * 新分页查询，默认查询七天内数据
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "用户七天日程查询")
    @ApiOperation(value = "用户七天日程查询", notes = "用户七天日程查询")
    @PostMapping(value = "/list")
    public Result<Map<String, List<QueryScheduleVO>>> queryPageList(@RequestBody RlSchedulePageDto dto) {
        Map<String, List<QueryScheduleVO>> result = rlScheduleService.queryPageList(dto);
        return Result.OK(result);
    }

    /**
     * 当天计划查询
     */
    @AutoLog(value = "用户当天计划查询")
    @ApiOperation(value = "用户当天计划查询", notes = "用户当天计划查询")
    @PostMapping(value = "/queryPlanDay")
    public Result<Page<QueryScheduleVO>> queryPlanDay(@RequestBody QueryIdDto dto) {
        List<QueryScheduleVO> result = rlScheduleService.queryPlanDay(dto);
        return Result.OK(new Page<QueryScheduleVO>().setRecords(result));
    }

    /**
     * 添加日程，默认状态为已读
     *
     * @param rlSchedule
     * @return
     */
    @AutoLog(value = "用户添加日程")
    @ApiOperation(value = "用户添加日程", notes = "用户添加日程")
    @PostMapping(value = "/addSchedule")
    public Result<String> addSchedule(@RequestBody RlSchedule rlSchedule) {
        rlScheduleService.saveSchedule(rlSchedule);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "用户编辑日程")
    @ApiOperation(value = "用户编辑日程", notes = "用户编辑日程")
    @PostMapping(value = "/updateSchedule")
    public Result<String> updateSchedule(@RequestBody RlSchedule rlSchedule) {
        if (ObjectUtil.isEmpty(rlSchedule.getId())) {
            throw new JeecgBootException("日程Id不能为空！");
        }
        rlScheduleService.removeScheduleById(String.valueOf(rlSchedule.getId()));
        rlSchedule.setId(null);
        rlScheduleService.saveSchedule(rlSchedule);
        return Result.OK("编辑成功！");
    }

    @AutoLog(value = "获得个人完成任务信息详情，默认获取前30条信息")
    @ApiOperation(value = "获得个人完成任务信息详情，默认获取前30条信息，state；10-待查收，20-待办，30-完成，60-超期，70-会议", notes = "获得个人完成任务信息详情，默认获取前30条信息")
    @PostMapping("/queryUserTaskInfo")
    public Result<Page<RlSchedule>> queryUserTaskInfo(@RequestBody QueryUserTaskInfoDto dto) {
        Page<RlSchedule> result = rlScheduleService.queryUserTaskInfo(dto);
        return Result.OK(result);
    }

    @AutoLog(value = "修改日程状态")
    @ApiOperation(value = "修改日程状态", notes = "修改日程状态")
    @PostMapping("/modifyScheduleState")
    public Result<String> modifyScheduleState(@RequestParam("id") Integer id, @RequestParam(name = "isComplete") String isComplete) {
        rlScheduleService.modifyScheduleState(id, isComplete);
        return Result.OK("日程状态修改成功！");
    }

    @AutoLog(value = "工作处理")
    @ApiOperation(value = "工作处理，注意type参数", notes = "工作处理")
    @PostMapping(value = "/workHandling")
    public Result<PageData<WorkHandlingVO>> workHandling(@RequestBody QueryTaskDto dto) {
        PageData<WorkHandlingVO> result = rlScheduleService.workHandling(dto);
        return Result.OK(result);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "rl_schedule-通过id删除")
    @ApiOperation(value = "rl_schedule-通过id删除", notes = "rl_schedule-通过id删除")
//	@RequiresPermissions("calendar:rl_schedule:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        rlScheduleService.removeScheduleById(id);
        return Result.OK("撤回成功!");
    }


    /**
     * 分页列表查询
     *
     * @param rlSchedule
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "rl_schedule-分页列表查询")
    @ApiOperation(value = "rl_schedule-分页列表查询", notes = "rl_schedule-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<RlSchedule>> queryPageList(RlSchedule rlSchedule, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
//		 QueryWrapper<RlSchedule> queryWrapper = QueryGenerator.initQueryWrapper(rlSchedule, req.getParameterMap());
        QueryWrapper<RlSchedule> queryWrapper = new QueryWrapper<>();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        String beginTime = req.getParameter("beginTime");
        String endTime = req.getParameter("endTime");
        String userId = req.getParameter("userId");
        if (StrUtil.isNotBlank(beginTime)) {
            queryWrapper.ge("begin_time", DateUtil.parse(beginTime, "yyyy-MM-dd HH:mm:ss"));
        }
        if (StrUtil.isNotBlank(endTime)) {
            queryWrapper.le("end_time", DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss"));
        }
        if (StrUtil.isNotBlank(userId)) {
            queryWrapper.eq("user_id", userId);
        }
        // 只查询当前用户的信息
        queryWrapper.eq("create_by", user.getUsername());
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("state", StateEnum.SCHEDULE.getCode());
        Page<RlSchedule> page = new Page<RlSchedule>(pageNo, pageSize);
        IPage<RlSchedule> pageList = rlScheduleService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    /*------------------------------------------------------------------------------------*/
    //以下为生成低代码
    /*------------------------------------------------------------------------------------*/


    /**
     * 添加
     *
     * @param rlSchedule
     * @return
     */
    @AutoLog(value = "rl_schedule-添加")
    @ApiOperation(value = "rl_schedule-添加", notes = "rl_schedule-添加")
//	@RequiresPermissions("calendar:rl_schedule:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody RlSchedule rlSchedule) {
        rlScheduleService.save(rlSchedule);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param rlSchedule
     * @return
     */
    @AutoLog(value = "rl_schedule-编辑")
    @ApiOperation(value = "rl_schedule-编辑", notes = "rl_schedule-编辑")
//	@RequiresPermissions("calendar:rl_schedule:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody RlSchedule rlSchedule) {
        rlScheduleService.updateById(rlSchedule);
        return Result.OK("编辑成功!");
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "rl_schedule-批量删除")
    @ApiOperation(value = "rl_schedule-批量删除", notes = "rl_schedule-批量删除")
//	@RequiresPermissions("calendar:rl_schedule:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.rlScheduleService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "rl_schedule-通过id查询")
    @ApiOperation(value = "rl_schedule-通过id查询", notes = "rl_schedule-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<RlSchedule> queryById(@RequestParam(name = "id", required = true) String id) {
        RlSchedule rlSchedule = rlScheduleService.getById(id);
        if (rlSchedule == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(rlSchedule);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param rlSchedule
     */
//    @RequiresPermissions("calendar:rl_schedule:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RlSchedule rlSchedule) {
        return super.exportXls(request, rlSchedule, RlSchedule.class, "rl_schedule");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
//    @RequiresPermissions("calendar:rl_schedule:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RlSchedule.class);
    }

}
